home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 January
/
macpower199701.bin
/
AMUG
/
Publishing_19
/
Alpha 6.5.sit
/
Tcl
/
Modes
/
htmlCustom.tcl
< prev
next >
Encoding:
Amiga
Atari
Commodore
DOS
FM Towns/JPY
Macintosh
Macintosh JP
NeXTSTEP
RISC OS/Acorn
UTF-8
Wrap
Text File
|
1996-08-15
|
36.7 KB
|
1,064 lines
|
[
TEXT/ALFA
]
#=============================================================================
#
# htmlCustom.tcl
#
# Part of HTML mode 1.2
#
# HTML custom elements.
#
# Author: Johan Linde <jl@theophys.kth.se>
#
# If you make improvements to this file, please share them!
#
#=============================================================================
#
# Defining new HTML elements.
#
proc htmlCustomNewElem {} {
global htmlElemAttrOptional1 htmlURLAttr htmlColorAttr htmlWindowAttr htmlElemAttrUsed
global PREFS htmlElemAttrRequired1 htmlElemAttrChoices1 htmlElemAttrNumber1
global htmlElemEventHandler1 htmlElemProc htmlElemKeyBinding htmlPlugins htmlElemAttrMore
global HTMLmodeVars specURL specColor specWindow htmlSpecURL htmlSpecColor htmlSpecWindow
global htmlVersion
set invalidInput 1
set values {"" 1 1 0 0 "" 0 0 0 0}
while {$invalidInput} {
set box "-t {New element} 10 10 100 25 -e [list [lindex $values 0]] 110 10 250 25 ¥
-c {Has closing tag} [lindex $values 1] 10 40 150 55 ¥
-t {Element type} 10 80 100 95 -r Normal [lindex $values 2] 10 100 100 115 ¥
-r {INPUT element with TYPE given above} [lindex $values 3] 10 120 300 135 ¥
-r {Plug-in} [lindex $values 4] 10 140 100 155 ¥
-t {Key binding} 10 180 90 195 -e [list [lindex $values 5]] 100 180 120 195 ¥
-c Shift [lindex $values 6] 10 210 60 225 ¥
-c Control [lindex $values 7] 80 210 150 225 ¥
-c Option [lindex $values 8] 160 210 220 225 ¥
-c Command [lindex $values 9] 230 210 320 225 ¥
-b OK 20 240 85 260 -b Cancel 105 240 170 260"
set values [eval [concat dialog -w 340 -h 270 $box]]
if {[lindex $values 11]} {return}
set element [string toupper [string trim [lindex $values 0]]]
set closingTag [lindex $values 1]
if {[lindex $values 2]} {
set elemType normal
} elseif {[lindex $values 3]} {
set elemType input
} else {
set elemType plugin
}
set elemKey [string toupper [string trim [lindex $values 5]]]
set keyStr ""
if {[lindex $values 6]} {append keyStr "<U"}
if {[lindex $values 7]} {append keyStr "<B"}
if {[lindex $values 8]} {append keyStr "<I"}
if {[lindex $values 9]} {append keyStr "<O"}
# Check that input is ok.
if {![string length $element]} {
alertnote "You must specify the element."
} elseif {[info exists htmlElemAttrOptional1($element)]} {
alertnote "The element $element is already defined."
return
} elseif {![regexp {^[_a-zA-Z0-9]+$} $element]} {
alertnote "Invalid characters in element name. For example, it may not contain spaces."
} elseif {[string length $elemKey] > 1} {
alertnote "You should only give one character for key binding."
} elseif {[string length $elemKey] && ($keyStr == "" || $keyStr == "<U")} {
alertnote "You must choose at least one of the modifiers control, option and command when you define a key binding."
} else {
set invalidInput 0
}
}
if {![string length $elemKey]} {
set keyStr ""
} else {
set elemKey "/$elemKey"
}
# Get the attributes
set allattributes [htmlGetCustomAttrs $element {}]
if {![string length $allattributes]} {return}
set optional [lindex $allattributes 0]
set AttrRequired [lindex $allattributes 1]
set AttrNumber [lindex $allattributes 2]
set AttrChoices [lindex $allattributes 3]
set EventHandler [lindex $allattributes 4]
set URL [lindex $allattributes 5]
set Color [lindex $allattributes 6]
set Window [lindex $allattributes 7]
# Get the layout.
if {$elemType != "normal" || !$closingTag} {
set customproc [htmlSetCustProc1 {0 0} $elemType $element]
} else {
set customproc [htmlSetCustProc2 {1 0 0 0} $element]
}
if {![string length $customproc]} {return}
# Save the element
message "Saving new elementノ"
set isfile [file exists $PREFS:HTMLadditions.tcl]
set fid [open $PREFS:HTMLadditions.tcl a+]
if {!$isfile} {puts $fid $htmlVersion}
puts $fid "$element ¥{set htmlElemKeyBinding($element) [list $keyStr$elemKey]¥}"
set htmlElemKeyBinding($element) $keyStr$elemKey
puts $fid "$element ¥{set htmlElemProc($element) [list $customproc]¥}"
set htmlElemProc($element) $customproc
foreach rcne [list AttrRequired AttrChoices AttrNumber EventHandler] {
if {[llength [set $rcne]]} {
puts $fid "$element ¥{set htmlElem${rcne}1($element) [list [set $rcne]]¥}"
set htmlElem${rcne}1($element) [set $rcne]
}
}
# Remove possible old versions of htmlElemAttrUsed and htmlElemAttrMore
if {[info exists htmlElemAttrUsed($element)]} {
unset htmlElemAttrUsed($element)
removeArrDef htmlElemAttrUsed $element
}
if {[info exists htmlElemAttrMore($element)]} {
unset htmlElemAttrMore($element)
removeArrDef htmlElemAttrMore $element
}
puts $fid "$element ¥{set htmlElemAttrOptional1($element) [list $optional]¥}"
set htmlElemAttrOptional1($element) $optional
foreach ucw [list URL Color Window] {
if {[llength [set $ucw]]} {
foreach a [set $ucw] {
puts $fid "$element ¥{lappend html${ucw}Attr $a¥}"
lappend html${ucw}Attr $a
}
}
}
if {$elemType == "plugin"} {
puts $fid "$element ¥{lappend htmlPlugins $element¥}"
lappend htmlPlugins $element
}
foreach ucw [list URL Color Window] {
if {[llength [set spec$ucw]]} {
puts $fid "$element ¥{lappend htmlSpec$ucw [set spec$ucw]¥}"
append htmlSpec$ucw " " [set spec$ucw]
}
}
close $fid
message "Inserting new element in menuノ"
htmlBuildMenu
if {$HTMLmodeVars(JavaScriptColoring)} {
regModeKeywords -a -k $HTMLmodeVars(tagColor) ¥
HTML [concat "<$element" "/$element" $AttrRequired $optional]
}
message "Done."
if {!$HTMLmodeVars(useBigWindows) && [llength $optional]} {htmlUseAttrs $element}
unset specURL
unset specColor
unset specWindow
}
# Returns a list of all attributes used in any HTML element.
proc htmlGetAllAttrs {} {
global htmlElemAttrOptional1 htmlElemAttrRequired1
set allHTMLelems [array names htmlElemAttrOptional1]
set allHTMLattrs ""
foreach elem $allHTMLelems {
if {[info exists htmlElemAttrRequired1($elem)]} {
foreach a $htmlElemAttrRequired1($elem) {
lappend allHTMLattrs $a
}
}
foreach a $htmlElemAttrOptional1($elem) {
lappend allHTMLattrs $a
}
}
return $allHTMLattrs
}
# Get attributes to custom element.
proc htmlGetCustomAttrs {element allattrs {nomore 1}} {
global htmlURLAttr htmlColorAttr htmlWindowAttr
global specURL specColor specWindow
set allHTMLattrs [htmlGetAllAttrs]
set optional {}
set AttrRequired {}
set AttrChoices {}
set AttrNumber {}
set EventHandler {}
set URL {}
set Color {}
set Window {}
set specURL {}
set specColor {}
set specWindow {}
set i 0
set dispAttr $allattrs
while {1} {
incr i
if {[catch {htmlCustomInpAttr $element $i $dispAttr $nomore} attribute]} {
if {$attribute != "Remove last!"} {return}
set toremove [lindex $dispAttr [expr [llength $dispAttr] - 1]]
set dispAttr [lreplace $dispAttr [expr [llength $dispAttr] - 1] [expr [llength $dispAttr] - 1]]
set allattrs [lreplace $allattrs [expr [llength $allattrs] - 1] [expr [llength $allattrs] - 1]]
set elemrm [lindex $toremove 0]
if {[lindex $toremove 1] == "(Flag)"} {
if {[set ind [lsearch -exact $AttrRequired $elemrm]] >=0} {
set AttrRequired [lreplace $AttrRequired $ind $ind]
} elseif {[set ind [lsearch -exact $optional $elemrm]] >=0} {
set optional [lreplace $optional $ind $ind]
}
} else {
foreach l [list optional AttrRequired AttrChoices AttrNumber EventHandler URL Color Window] {
set tmp {}
foreach m [set $l] {
if {![string match "${elemrm}=*" $m]} {
lappend tmp $m
}
}
set $l $tmp
}
}
foreach l [list URL Color Window] {
if {[set where [lsearch -exact [set spec$l] "${element}=[string trimright $elemrm =]"]] >= 0 || ¥
[set where [lsearch -exact [set spec$l] "${element}!=[string trimright $elemrm =]"]] >= 0} {
set spec$l [lreplace [set spec$l] $where $where]
}
}
incr i -2
continue
}
if {![string length $attribute]} {break}
if {[lsearch -exact [string toupper $allattrs] [string toupper [lindex $attribute 0]]] >= 0} {
alertnote "$element already has an attribute '[lindex $attribute 0]'."
incr i -1
} else {
if {[catch {htmlCustomAttrFix $element [lindex $attribute 0] ¥
[lindex $attribute 1] $allHTMLattrs} thisattr]} {
incr i -1
continue
}
lappend allattrs [string trimright [lindex $thisattr 0] =]
set attr [lindex $thisattr 0]
set thistype [lindex $thisattr 1]
if {[lindex $attribute 2]} {
lappend AttrRequired $attr
} elseif {$thistype != "Event handler"} {
lappend optional $attr
} else {
lappend EventHandler $attr
}
if {$thistype == "Choices"} {
foreach c [lindex $thisattr 2] {
lappend AttrChoices "$attr$c"
}
} elseif {$thistype == "Number"} {
lappend AttrNumber "$attr[lindex $thisattr 2]"
} elseif {$thistype == "URL" && [lsearch -exact $htmlURLAttr $attr] < 0 && [lsearch -exact $allHTMLattrs $attr] < 0} {
lappend URL $attr
} elseif {$thistype == "Color" && [lsearch -exact $htmlColorAttr $attr] < 0 && [lsearch -exact $allHTMLattrs $attr] < 0} {
lappend Color $attr
} elseif {$thistype == "Window" && [lsearch -exact $htmlWindowAttr $attr] < 0 && [lsearch -exact $allHTMLattrs $attr] < 0} {
lappend Window $attr
}
lappend dispAttr "[string trimright $attr =] (${thistype})"
}
}
return [list $optional $AttrRequired $AttrNumber $AttrChoices $EventHandler $URL $Color $Window]
}
# Dialog for giving a new attribute.
proc htmlCustomInpAttr {element num allattrs nomore} {
set typeList [list Other Number Choices Flag URL Color Window {Event handler}]
set values {0 0 {} Other 0}
set invalidInput 1
while {$invalidInput} {
set box "-t {Attribute $num for $element} 10 10 330 25 ¥
-e [list [lindex $values 2]] 10 40 150 55 ¥
-t Type: 170 40 205 55 ¥
-m [list [concat [list [lindex $values 3]] $typeList]] ¥
210 40 330 55 -c Required [lindex $values 4] 10 70 130 85"
if {$num > 1} {append box " -b {Remove last} 340 100 450 120"}
if {$nomore || $num > 1} {append box " -b {No more attributes} 340 70 480 90"}
set wi 10
set ht 120
if {[llength $allattrs]} {
append box " -t {All attributes} 10 100 200 115"
foreach ch $allattrs {
append box " -t [list $ch] $wi $ht [expr $wi + 195] [expr $ht + 15]"
incr wi 200
if {$wi == 410} {
set wi 10
incr ht 20
}
}
}
if {$wi == 210} {incr ht 20}
if {$ht < 130} {set ht 130}
set values [eval [concat dialog -w 490 -h $ht ¥
-b OK 340 10 405 30 -b Cancel 340 40 405 60 $box]]
if {[lindex $values 1]} {
error "Cancel"
} elseif {$num > 1 && [lindex $values 5]} {
error "Remove last!"
} elseif {[lindex $values 0]} {
set thisattr [string trim [lindex $values 2]]
set thistype [lindex $values 3]
if {$thistype != "Event handler"} {set thisattr [string toupper $thisattr]}
set required [lindex $values 4]
if {![regexp {^[_a-zA-Z0-9]*$} $thisattr]} {
alertnote "Invalid characters in attribute. For example, it may not contain spaces."
} elseif {[string length $thisattr]} {
if {$required && $thistype == "Event handler"} {
alertnote "Event handlers cannot be required attributes. It will be optional."
set required 0
}
set invalidInput 0
}
} else {
return
}
}
return [list $thisattr $thistype $required]
}
# Dialogs to give more info about new attributes.
proc htmlCustomAttrFix {element attr type allHTMLattrs {allchoices ""}} {
global htmlURLAttr htmlColorAttr htmlWindowAttr
global specURL specColor specWindow
# Check for special case with URL etc. if not called from htmlCustomNewChoice
# (then allchoices has length >0)
foreach ucw [list URL Color Window] {
if {[lsearch -exact [set html${ucw}Attr] "$attr="] >= 0 && $type != $ucw && ![llength $allchoices]} {
lappend spec$ucw "$element!=$attr"
}
}
switch $type {
Other {return [list "$attr=" $type]}
Number {
set values {0 0 0 {} 0}
while {1} {
set box "-t {Range for $attr} 60 10 290 25 -t {Minvalue:} 10 40 100 55 ¥
-e [list [lindex $values 2]] 110 40 130 55 -t {Maxvalue:} 150 40 240 55 ¥
-e [list [lindex $values 3]] 250 40 270 55 -c {Value may be given in percent} ¥
[lindex $values 4] 10 65 250 80"
set values [eval [concat dialog -w 300 -h 120 ¥
-b OK 20 90 85 110 -b Cancel 105 90 170 110 $box]]
set min [string trim [lindex $values 2]]
set max [string trim [lindex $values 3]]
set percent [lindex $values 4]
if {[lindex $values 1]} {
error "Cancel"
} elseif {[lindex $values 0]} {
if {![htmlIsInteger $min]} {
alertnote "A minimum value must be specified."
} elseif {[string length $max] && ![htmlIsInteger $max]} {
alertnote "Not a valid number for maximum value."
} elseif {[string length $max] && $max < $min} {
alertnote "Maxvalue is smaller than minvalue."
} else {
break
}
}
}
set number "$min:"
if {[string length $max]} {
append number "$max:"
} else {
append number "i:"
}
if {$percent} {
append number "%"
} else {
append number "n"
}
return [list "$attr=" $type $number]
}
Choices {
set i 0
set choices {}
while {1} {
incr i
set values {0 0 {}}
set invalidInput 1
while {$invalidInput} {
set box "-t {Choice $i for $attr} 10 10 210 25 ¥
-e [list [lindex $values 2]] 10 40 200 55"
if {$i > 1} {append box " -b {No more choices} 220 70 340 90 -b {Remove last} 220 100 340 120"}
set wi 10
set ht 90
if {[llength $allchoices]} {
append box " -t {All choices} 10 70 200 85"
foreach ch $allchoices {
append box " -t $ch $wi $ht [expr $wi + 95] [expr $ht + 15]"
incr wi 100
if {$wi == 210} {
set wi 10
incr ht 20
}
}
}
if {$wi == 110} {incr ht 20}
if {$ht < 130} {set ht 130}
set values [eval [concat dialog -w 350 -h $ht ¥
-b OK 220 10 285 30 -b Cancel 220 40 285 60 ¥
$box]]
if {[lindex $values 1]} {
error "Cancel"
} elseif {$i > 1 && [lindex $values 3] } {
return [list "$attr=" $type $choices]
} elseif {$i > 1 && [lindex $values 4]} {
incr i -1
set choices [lreplace $choices [expr [llength $choices] - 1] [expr [llength $choices] - 1]]
set allchoices [lreplace $allchoices [expr [llength $allchoices] - 1] [expr [llength $allchoices] - 1]]
} elseif {[lindex $values 0]} {
set thischoice [string toupper [string trim [lindex $values 2]]]
if {![regexp {^[_a-zA-Z0-9]*$} $thischoice]} {
alertnote "Invalid characters in choice. For example, it may not contain spaces."
} elseif {[string length $thischoice]} {
if {[lsearch -exact $allchoices $thischoice] >=0 } {
alertnote "$attr already has a choice '$thischoice'."
} else {
set invalidInput 0
}
}
}
}
lappend choices $thischoice
lappend allchoices $thischoice
}
}
Flag {return [list $attr $type]}
URL {
if {[lsearch -exact $htmlURLAttr "$attr="] < 0 && [lsearch -exact $allHTMLattrs "$attr="] >= 0} {
lappend specURL "${element}=$attr"
}
return [list "$attr=" $type]
}
Color {
if {[lsearch -exact $htmlColorAttr "$attr="] < 0 && [lsearch -exact $allHTMLattrs "$attr="] >= 0} {
lappend specColor "${element}=$attr"
}
return [list "$attr=" $type]
}
Window {
if {[lsearch -exact $htmlWindowAttr "$attr="] < 0 && [lsearch -exact $allHTMLattrs "$attr="] >= 0} {
lappend specWindow "${element}=$attr"
}
return [list "$attr=" $type]
}
"Event handler" {
return [list "$attr=" $type]
}
}
}
proc htmlSetCustProc1 {values elemType element} {
set box "-t {Layout} 80 10 180 25 ¥
-c {Always a new line before tag.} [lindex $values 0] 10 40 225 55 ¥
-c {Always a new line after tag.} [lindex $values 1] 10 60 225 75 ¥
-b OK 20 90 85 110 -b Cancel 105 90 170 110"
set values [eval [concat dialog -w 230 -h 120 $box]]
if {[lindex $values 3]} {return}
switch $elemType {
normal {set customproc "htmlBuildOpening $element"}
input {set customproc "htmlBuildInputElem $element"}
plugin {set customproc "htmlBuildOpening EMBED"}
}
lappend customproc [lindex $values 0] [lindex $values 1]
if {$elemType == "plugin"} {lappend customproc $element}
return $customproc
}
proc htmlSetCustProc2 {values element} {
set box "-t {Layout} 80 10 180 25 ¥
-r {text<TAG>text</TAG>text} [lindex $values 0] 10 40 200 60 ¥
-r {text¥r<TAG>text</TAG>¥rtext} [lindex $values 1] 10 70 150 130 ¥
-r {blank line¥r<TAG>text</TAG>¥rblank line} [lindex $values 2] 10 140 150 200 ¥
-r {blank line¥r<TAG>¥rtext¥r</TAG>¥rblank line} [lindex $values 3] 10 210 150 310"
set values [eval [concat dialog -w 200 -h 350 ¥
-b OK 20 320 85 340 -b Cancel 105 320 170 340 $box]]
if {[lindex $values 1]} {return}
if {[lindex $values 2]} {set customproc "htmlBuildElem $element"}
if {[lindex $values 3]} {set customproc "htmlBuildCRElem $element"}
if {[lindex $values 4]} {set customproc "htmlBuildCRElem $element 1"}
if {[lindex $values 5]} {set customproc "htmlBuildCR2Elem $element"}
return $customproc
}
# Add new attributes to an element.
proc htmlCustomNewAttr {} {
global htmlElemAttrOptional1 htmlURLAttr htmlColorAttr htmlWindowAttr
global PREFS htmlElemAttrRequired1 htmlElemAttrChoices1 htmlElemAttrNumber1
global htmlElemEventHandler1 HTMLmodeVars htmlSpecURL htmlSpecColor htmlSpecWindow
global specURL specColor specWindow htmlVersion
if {[catch {listpick -p "Select element to add attributes to." ¥
[lsort [array names htmlElemAttrOptional1]]} element] || ¥
![string length $element]} {return}
set allattrs {}
foreach e [htmlGetRequired $element] {
lappend allattrs [string trimright $e =]
}
foreach e [htmlGetOptional $element] {
lappend allattrs [string trimright $e =]
}
if {[info exists htmlElemEventHandler1($element)]} {
foreach e $htmlElemEventHandler1($element) {
lappend allattrs [string trimright $e =]
}
}
set attributes [htmlGetCustomAttrs $element $allattrs 0]
if {![string length [join $attributes ""]]} {return}
set AttrOptional [lindex $attributes 0]
set AttrRequired [lindex $attributes 1]
set AttrNumber [lindex $attributes 2]
set AttrChoices [lindex $attributes 3]
set EventHandler [lindex $attributes 4]
set URL [lindex $attributes 5]
set Color [lindex $attributes 6]
set Window [lindex $attributes 7]
if {[regexp { } $element]} {
set arg "¥[list $element¥]"
} else {
set arg $element
}
if {![llength [htmlGetOptional $element]]} {
set rmenu 1
} else {
set rmenu 0
}
# Save the element
message "Saving new attributesノ"
set isfile [file exists $PREFS:HTMLadditions.tcl]
set fid [open $PREFS:HTMLadditions.tcl a+]
if {!$isfile} {puts $fid $htmlVersion}
foreach rcne [list AttrRequired AttrChoices AttrNumber EventHandler AttrOptional] {
if {[string length [set $rcne]]} {
puts $fid "[list $element] ¥{lappend htmlElem${rcne}1($arg) [set $rcne]¥}"
append htmlElem${rcne}1($element) " " [set $rcne]
}
}
foreach ucw [list URL Color Window] {
if {[string length [set $ucw]]} {
foreach a [set $ucw] {
puts $fid "[list $element] ¥{lappend html${ucw}Attr $a¥}"
lappend html${ucw}Attr $a
}
}
}
foreach ucw [list URL Color Window] {
if {[llength [set spec$ucw]]} {
puts $fid "[list $element] ¥{lappend htmlSpec$ucw [set spec$ucw]¥}"
append htmlSpec$ucw " " [set spec$ucw]
}
}
close $fid
if {$rmenu} {htmlBuildMenu}
if {$HTMLmodeVars(JavaScriptColoring)} {
regModeKeywords -a -k $HTMLmodeVars(tagColor) ¥
HTML [concat $AttrRequired $AttrOptional]
}
unset specURL
unset specColor
unset specWindow
message "Done."
if {!$HTMLmodeVars(useBigWindows) && [llength [htmlGetOptional $element]]} {htmlUseAttrs $element}
}
# Add new choices to an attribute with predefined choices.
proc htmlCustomNewChoice {} {
global htmlElemAttrChoices1 PREFS htmlVersion
if {[catch {listpick -p "Select element to add choices to." ¥
[lsort [array names htmlElemAttrChoices1]]} element] || ¥
![string length $element]} {return}
set choiceatts ""
foreach e $htmlElemAttrChoices1($element) {
regexp {[^=]*} $e attr
if {[lsearch $choiceatts $attr] < 0} {lappend choiceatts $attr}
}
if {[catch {listpick -p "Select attribute to add choices to." [lsort $choiceatts]} attr] || ¥
![string length $attr]} {return}
foreach c $htmlElemAttrChoices1($element) {
if {[string match "${attr}=*" $c]} {
lappend allchoices [string range $c [expr [string length $attr] + 1] end]
}
}
set newchoices [htmlCustomAttrFix $element $attr Choices [htmlGetAllAttrs] $allchoices]
foreach c [lindex $newchoices 2] {
lappend choices "${attr}=$c"
}
if {[regexp { } $element]} {
set arg "¥[list $element¥]"
} else {
set arg $element
}
# Save the choices
message "Saving new choicesノ"
set isfile [file exists $PREFS:HTMLadditions.tcl]
set fid [open $PREFS:HTMLadditions.tcl a+]
if {!$isfile} {puts $fid $htmlVersion}
puts $fid "[list $element] ¥{lappend htmlElemAttrChoices1($arg) $choices¥}"
append htmlElemAttrChoices1($element) " " $choices
close $fid
}
#
# Change key binding for a custom element.
#
proc htmlCustomChangeKey {} {
global htmlElemKeyBinding PREFS
if {![info exists htmlElemKeyBinding]} {
alertnote "No custom elements are defined."
return
}
if {[catch {listpick -p "Select element to change key binding for." ¥
[lsort [array names htmlElemKeyBinding]]} elem] || ![string length $elem]} {return}
set keystr $htmlElemKeyBinding($elem)
if {[string length $keystr]} {
set values "0 0 [string range $keystr [expr [string length $keystr] - 1] end]"
set keystr [string range $keystr 0 [expr [string length $keystr] - 3]]
lappend values [regexp {U} $keystr]
lappend values [regexp {B} $keystr]
lappend values [regexp {I} $keystr]
lappend values [regexp {O} $keystr]
} else {
set values {0 0 {} 0 0 0 0}
}
while {1} {
set box "-t {Key binding for $elem} 40 10 290 25 ¥
-t Key 10 40 40 55 -e [list [lindex $values 2]] 50 40 70 55 ¥
-c Shift [lindex $values 3] 10 60 60 75 ¥
-c Control [lindex $values 4] 80 60 150 75 ¥
-c Option [lindex $values 5] 160 60 220 75 ¥
-c Command [lindex $values 6] 230 60 315 75"
set values [eval [concat dialog -w 320 -h 120 ¥
-b OK 20 90 85 110 -b Cancel 105 90 170 110 $box]]
if {[lindex $values 1]} {return}
set elemKey [string toupper [string trim [lindex $values 2]]]
set keyStr ""
if {[lindex $values 3]} {append keyStr "<U"}
if {[lindex $values 4]} {append keyStr "<B"}
if {[lindex $values 5]} {append keyStr "<I"}
if {[lindex $values 6]} {append keyStr "<O"}
if {[string length $elemKey] > 1} {
alertnote "You should only give one character for key binding."
} elseif {[string length $elemKey] && ($keyStr == "" || $keyStr == "<U")} {
alertnote "You must choose at least one of the modifiers control, option and command when you define a key binding."
} else {
break
}
}
if {![string length $elemKey]} {
set keyStr ""
} else {
set elemKey "/$elemKey"
}
if {![file exists $PREFS:HTMLadditions.tcl]} {
alertnote "Cannot find 'HTMLadditions.tcl'. Key binding cannot be changed."
return
}
message "Redefining key bindingノ"
set fid [open $PREFS:HTMLadditions.tcl r]
set filecont [string trimright [read $fid] "¥n"]
close $fid
foreach line [split $filecont "¥n"] {
if {[lindex $line 0] == $elem && [regexp {htmlElemKeyBinding} $line]} {
append newlines "$elem ¥{set htmlElemKeyBinding($elem) [list $keyStr$elemKey]¥}¥n"
} else {
append newlines "$line¥n"
}
}
set fid [open $PREFS:HTMLadditions.tcl w]
puts -nonewline $fid $newlines
close $fid
set htmlElemKeyBinding($elem) $keyStr$elemKey
htmlBuildMenu
message "Done."
}
#
# Change type and layout for a custom element.
#
proc htmlCustomChangeType {} {
global htmlElemKeyBinding htmlElemProc PREFS htmlPlugins
if {![info exists htmlElemKeyBinding]} {
alertnote "No custom elements are defined."
return
}
if {[catch {listpick -p "Select element to change type and layout for." ¥
[lsort [array names htmlElemKeyBinding]]} elem] || ![string length $elem]} {return}
set eproc $htmlElemProc($elem)
set proctype [lindex $eproc 0]
if {$proctype == "htmlBuildOpening" || $proctype == "htmlBuildInputElem"} {
if {[lindex $eproc 1] == "EMBED"} {
set type plugin
} else {
set type normal
}
if {$proctype == "htmlBuildInputElem"} {set type input}
set closing 0
set values "[lindex $eproc 2] [lindex $eproc 3]"
} else {
set type normal
set closing 1
if {$proctype == "htmlBuildElem"} {set values {1 0 0 0}}
if {$proctype == "htmlBuildCRElem" && [llength $eproc] == 2} {set values {0 1 0 0}}
if {$proctype == "htmlBuildCRElem" && [llength $eproc] == 3} {set values {0 0 1 0}}
if {$proctype == "htmlBuildCR2Elem"} {set values {0 0 0 1}}
}
set box "-t $elem 100 10 300 25 ¥
-c {Has closing tag} $closing 10 40 150 55 ¥
-t {Element type} 10 80 100 95 -r Normal [regexp {normal} $type] 10 100 100 115 ¥
-r {INPUT element with TYPE given above} [regexp {input} $type] 10 120 300 135 ¥
-r {Plug-in} [regexp {plugin} $type] 10 140 100 155 ¥
-b OK 20 170 85 190 -b Cancel 105 170 170 190"
set typeval [eval [concat dialog -w 310 -h 200 $box]]
if {[lindex $typeval 5]} {return}
set newclosing [lindex $typeval 0]
if {[lindex $typeval 1]} {set newtype normal}
if {[lindex $typeval 2]} {set newtype input; set newclosing 0}
if {[lindex $typeval 3]} {set newtype plugin; set newclosing 0}
if {$newclosing} {
if {$newclosing != $closing} {set values {1 0 0 0}}
set customproc [htmlSetCustProc2 $values $elem]
} else {
if {$newclosing != $closing} {set values {0 0}}
set customproc [htmlSetCustProc1 $values $newtype $elem]
}
if {![string length $customproc]} {return}
if {![file exists $PREFS:HTMLadditions.tcl]} {
alertnote "Cannot find 'HTMLadditions.tcl'. Type and layout cannot be changed."
return
}
message "Redefining type and layoutノ"
set fid [open $PREFS:HTMLadditions.tcl r]
set filecont [string trimright [read $fid] "¥n"]
close $fid
foreach line [split $filecont "¥n"] {
if {[lindex $line 0] == $elem && [regexp {htmlElemProc} $line]} {
append newlines "$elem ¥{set htmlElemProc($elem) [list $customproc]¥}¥n"
} elseif {$type == "plugin" && $newtype != "plugin" && [lindex $line 0] == $elem && ¥
[regexp {htmlPlugins} $line]} {
set where [lsearch -exact $htmlPlugins $elem]
set htmlPlugins [lreplace $htmlPlugins $where $where]
} else {
append newlines "$line¥n"
}
}
if {$newtype == "plugin" && $type != "plugin"} {
lappend htmlPlugins $elem
append newlines "$elem ¥{lappend htmlPlugins $elem¥}¥n"
}
set fid [open $PREFS:HTMLadditions.tcl w]
puts -nonewline $fid $newlines
close $fid
set htmlElemProc($elem) $customproc
message "Done."
}
# Remove custom element ot additions to an element.
proc htmlCustomRemove {} {
global htmlElemAttrOptional1 htmlURLAttr htmlColorAttr htmlWindowAttr
global PREFS htmlElemAttrRequired1 htmlElemAttrChoices1 htmlElemAttrNumber1
global htmlElemEventHandler1 htmlElemProc htmlElemKeyBinding htmlPlugins
global htmlSpecURL htmlSpecColor htmlSpecWindow htmlVersion
if {![file exists $PREFS:HTMLadditions.tcl]} {
if {[info exists htmlElemKeyBinding]} {
alertnote "Cannot find 'HTMLadditions.tcl'. Custom additions cannot be removed."
} else {
alertnote "No custom additions has been made."
}
return
}
set fid [open $PREFS:HTMLadditions.tcl r]
set additions [string trimright [read $fid] "¥n"]
close $fid
set elems ""
foreach line [lrange [split $additions "¥n"] 1 end] {
set element [lindex $line 0]
if {[lsearch -exact $elems $element] < 0} {lappend elems $element}
}
if {[catch {listpick -p "Select element to remove additions from." [lsort $elems]} element] || ¥
![string length $element] || [askyesno "Remove additions from $element?"] == "no"} {return}
# Perhaps rebuild menu for if old elem and no optional attrs after removal.
if {[llength [htmlGetOptional $element]]} {
set rmenu 1
} else {
set rmenu 0
}
message "Removing additions to $elementノ"
set isNewElem [info exists htmlElemKeyBinding($element)]
# If new element, unset all its variables.
if {$isNewElem} {
catch {unset htmlElemAttrRequired1($element)}
catch {unset htmlElemAttrChoices1($element)}
catch {unset htmlElemAttrNumber1($element)}
catch {unset htmlElemAttrOptional1($element)}
catch {unset htmlElemEventHandler1($element)}
set tmpkey $htmlElemKeyBinding($element)
catch {unset htmlElemKeyBinding($element)}
catch {unset htmlElemProc($element)}
set isPlugin [lsearch -exact $htmlPlugins $element]
if {$isPlugin >=0 } {set htmlPlugins [lreplace $htmlPlugins $isPlugin $isPlugin]}
if {![llength [array names htmlElemKeyBinding]]} {
catch {unset htmlElemKeyBinding}
if {[string length $tmpkey]} {
set key [string tolower [string range $tmpkey [expr [string length $tmpkey] - 1] end]]
set mods ""
foreach m [split [string range $tmpkey 1 [expr [string length $tmpkey] - 3]] < ] {
if {$m == "B"} {append mods z}
if {$m == "I"} {append mods o}
if {$m == "U"} {append mods s}
if {$m == "O"} {append mods c}
}
catch {unbind '$key' <$mods> {} HTML}
}
}
if {![llength [array names htmlElemProc]]} {catch {unset htmlElemProc}}
}
set newlines ""
foreach line [lrange [split $additions "¥n"] 1 end] {
set command [lindex $line 1]
if {[lindex $line 0] != $element} {
append newlines "$line¥n"
} elseif {[lindex $command 0] == "lappend"} {
set var [lindex $command 1]
# Remove from URL, Color and Window lists.
foreach ucw [list URL Color Window] {
if {$var == "html${ucw}Attr"} {
lappend ${ucw}maybe [lindex $command 2]
set where [lsearch -exact [set html${ucw}Attr] [lindex $command 2]]
set html${ucw}Attr [lreplace [set html${ucw}Attr] $where $where]
}
if {$var == "htmlSpec${ucw}"} {
foreach c [lrange $command 2 end] {
set where [lsearch -exact [set htmlSpec${ucw}] $c]
set htmlSpec${ucw} [lreplace [set htmlSpec${ucw}] $where $where]
}
}
}
# If added attribute to old element, remove attribute
if {!$isNewElem && $var != "htmlURLAttr" && $var != "htmlColorAttr" && ¥
$var != "htmlWindowAttr" && $var != "htmlSpecURL" && $var != "htmlSpecColor" && ¥
$var != "htmlSpecWindow"} {
regexp {([^¥(]+)¥(([^¥)]+)¥)[ ]+(.+)} [lrange $command 1 end] dummy var arg added
foreach c $added {
set where [lsearch -exact [set ${var}($element)] $c]
set ${var}($element) [lreplace [set ${var}($element)] $where $where]
}
}
}
}
# Unset empty lists for old variables.
if {!$isNewElem} {
foreach c [list AttrRequired AttrChoices AttrNumber EventHandler] {
if {[info exists html${c}1($element)] && ![llength html${c}1($element)]} {
unset html${c}1($element)
}
}
}
# URL, Color or Window attributes just removed
# should be replaced if they are used by some other element.
foreach ucw [list URL Color Window] {
if {[info exists ${ucw}maybe]} {
append newlines [htmlUCWmaybe $ucw [set ${ucw}maybe]]
}
}
if {[string length $newlines]} {
set fid [open $PREFS:HTMLadditions.tcl w]
puts -nonewline $fid "$htmlVersion¥n$newlines"
close $fid
} else {
removeFile $PREFS:HTMLadditions.tcl
}
if {$isNewElem || ($rmenu && ![llength [htmlGetOptional $element]])} {htmlBuildMenu}
message "Done."
}
proc htmlUCWmaybe {ucw maybe} {
global htmlElemAttrRequired1 htmlElemAttrOptional1 htmlSpecURL htmlSpecColor htmlSpecWindow
global htmlURLAttr htmlColorAttr htmlWindowAttr
set newlines ""
foreach m $maybe {
set foundit 0
foreach e [array names htmlElemAttrRequired1] {
if {[lsearch -exact $htmlElemAttrRequired1($e) $m] >= 0 && ¥
[lsearch -exact [set htmlSpec$ucw] "$e!=[string trimright $m =]"] < 0} {
append newlines "[list $e] ¥{lappend html${ucw}Attr $m¥}¥n"
lappend html${ucw}Attr $m
set foundit 1
break
}
}
if {$foundit} {continue}
foreach e [array names htmlElemAttrOptional1] {
if {[lsearch -exact $htmlElemAttrOptional1($e) $m] >= 0 && ¥
[lsearch -exact [set htmlSpec$ucw] "$e!=[string trimright $m =]"] < 0} {
append newlines "[list $e] ¥{lappend html${ucw}Attr $m¥}¥n"
lappend html${ucw}Attr $m
break
}
}
}
return $newlines
}
# Remove custom element ot additions to an element.
proc htmlCustomRemoveAttrs {} {
global htmlElemAttrOptional1 htmlURLAttr htmlColorAttr htmlWindowAttr
global PREFS htmlElemAttrRequired1 htmlElemAttrChoices1 htmlElemAttrNumber1
global htmlElemEventHandler1
global htmlSpecURL htmlSpecColor htmlSpecWindow htmlVersion
if {![file exists $PREFS:HTMLadditions.tcl]} {
if {[info exists htmlElemKeyBinding]} {
alertnote "Cannot find 'HTMLadditions.tcl'. Custom additions cannot be removed."
} else {
alertnote "No custom additions has been made."
}
return
}
set fid [open $PREFS:HTMLadditions.tcl r]
set additions [string trimright [read $fid] "¥n"]
close $fid
set elems ""
foreach line [lrange [split $additions "¥n"] 1 end] {
set element [lindex $line 0]
if {[lsearch -exact $elems $element] < 0 && ¥
([llength [concat [htmlGetRequired $element] [htmlGetOptional $element]]] || ¥
[info exists htmlElemEventHandler1($element)])} {
lappend elems $element
}
}
if {[catch {listpick -p "Select element to remove attributes from." [lsort $elems]} element] || ¥
![string length $element]} {return}
set allatts {}
foreach line [lrange [split $additions "¥n"] 1 end] {
set command [lindex $line 1]
if {[lindex $line 0] == $element} {
regexp {([^¥(]+)¥(([^¥)]+)¥)[ ]+(.+)} [lrange $command 1 end] dummy var arg added
set added [string trimleft [string trimright $added ¥}] ¥{]
if {$var == "htmlElemAttrRequired1" || $var == "htmlElemAttrOptional1" || $var == "htmlElemEventHandler1"} {
foreach c $added {
if {[lsearch -exact $allatts [string trimright $c =]] < 0} {
lappend allatts [string trimright $c =]
}
}
} elseif {$var == "htmlElemAttrChoices1"} {
foreach c $added {
regexp {[^=]+} $c tmp
if {[lsearch -exact $allatts $tmp] < 0} {
lappend allatts $tmp
}
}
}
}
}
if {[catch {listpick -p "Select attributes to remove." -l [lsort $allatts]} attrs] || ¥
![string length $attrs]} {return}
# Perhaps rebuild menu for if old elem and no optional attrs after removal.
if {[llength [htmlGetOptional $element]]} {
set rmenu 1
} else {
set rmenu 0
}
message "Removing attributes from $elementノ"
set newlines ""
foreach line [lrange [split $additions "¥n"] 1 end] {
set command [lindex $line 1]
if {[lindex $line 0] != $element} {
append newlines "$line¥n"
} else {
set var [lindex $command 1]
# Remove from URL, Color and Window lists.
foreach ucw [list URL Color Window] {
if {$var == "html${ucw}Attr"} {
if {[lsearch -exact $attrs [string trimright [lindex $command 2] =]] >= 0} {
lappend ${ucw}maybe [lindex $command 2]
set where [lsearch -exact [set html${ucw}Attr] [lindex $command 2]]
set html${ucw}Attr [lreplace [set html${ucw}Attr] $where $where]
} else {
append newlines "$line¥n"
}
}
if {$var == "htmlSpec${ucw}"} {
set tmpadd [lrange $command 2 end]
foreach c $tmpadd {
regexp {[^!=]+!?=(.*)} $c dum tmp
if {[lsearch -exact $attrs $tmp] >= 0} {
set where [lsearch -exact [set htmlSpec${ucw}] $c]
set htmlSpec${ucw} [lreplace [set htmlSpec${ucw}] $where $where]
set where [lsearch -exact $tmpadd $c]
set tmpadd [lreplace $tmpadd $where $where]
}
}
if {[llength $tmpadd]} {append newlines "[list $element] ¥{lappend htmlSpec${ucw} $tmpadd¥}¥n"}
}
}
if {[lsearch {htmlURLAttr htmlColorAttr htmlWindowAttr htmlSpecURL htmlSpecColor htmlSpecWindow htmlPlugins} $var] < 0 && ¥
![string match "htmlElemKeyBinding*" $var] && ![string match "htmlElemProc*" $var]} {
regexp {([^¥(]+)¥(([^¥)]+)¥)[ ]+(.+)} [lrange $command 1 end] dummy var arg added
set added [string trimleft [string trimright $added ¥}] ¥{]
foreach c $added {
regexp {[^=]+} $c tmp
if {[lsearch -exact $attrs $tmp] >= 0} {
set where [lsearch -exact [set ${var}($element)] $c]
set ${var}($element) [lreplace [set ${var}($element)] $where $where]
set where [lsearch -exact $added $c]
set added [lreplace $added $where $where]
}
}
if {[llength $added] || ([lindex $command 0] == "set" && $var == "htmlElemAttrOptional1")} {
if {[lindex $command 0] == "set"} {set added [list $added]}
append newlines "[list $element] ¥{[lindex $command 0] ${var}($arg) $added¥}¥n"
}
}
if {[string match "htmlElemKeyBinding*" $var] || [string match "htmlElemProc*" $var]} {
append newlines "$line¥n"
}
}
}
# Unset empty lists.
foreach c [list AttrRequired AttrChoices AttrNumber EventHandler] {
if {[info exists html${c}1($element)] && ![llength html${c}1($element)]} {
unset html${c}1($element)
}
}
# URL, Color or Window attributes just removed
# should be replaced if they are used by some other element.
foreach ucw [list URL Color Window] {
if {[info exists ${ucw}maybe]} {
append newlines [htmlUCWmaybe $ucw [set ${ucw}maybe]]
}
}
if {[string length $newlines]} {
set fid [open $PREFS:HTMLadditions.tcl w]
puts -nonewline $fid "$htmlVersion¥n$newlines"
close $fid
} else {
removeFile $PREFS:HTMLadditions.tcl
}
if {$rmenu && ![llength [htmlGetOptional $element]]} {htmlBuildMenu}
message "Done."
}